package com.jsh.frame.myframe2.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 配置缓存
 */
public class PrefsUtil {

    public PrefsUtil() {
    }

    /**
     * 写入配置文件
     *
     * @param context
     * @param key
     * @param value
     */
    public static boolean write(Context context, String key, String value) {
        if (context == null || key == null || value == null) {
            return false;
        }
        SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(context);
        Editor edit = spf.edit();
        edit.putString(key, value);
        return edit.commit();
    }

    public static boolean write(Context context, String key, boolean value) {
        if (context == null || key == null) {
            return false;
        }
        SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(context);
        Editor edit = spf.edit();
        edit.putBoolean(key, value);
        return edit.commit();
    }

    public static boolean write(Context context, String key, float value) {
        if (context == null || key == null) {
            return false;
        }
        SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(context);
        Editor edit = spf.edit();
        edit.putFloat(key, value);
        return edit.commit();
    }

    public static boolean write(Context context, String key, int value) {
        if (context == null || key == null) {
            return false;
        }
        SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(context);
        Editor edit = spf.edit();
        edit.putInt(key, value);
        return edit.commit();
    }

    public static boolean write(Context context, String key, long value) {
        if (context == null || key == null) {
            return false;
        }
        SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(context);
        Editor edit = spf.edit();
        edit.putLong(key, value);
        return edit.commit();
    }

    /**
     * 移除某个key值已经对应的值
     *
     * @param context
     * @param key
     */
    public static void remove(Context context, String key) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        SharedPreferencesCompat.apply(editor);
    }

    /**
     * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
     *
     * @author zhy
     */
    private static class SharedPreferencesCompat {
        private static final Method sApplyMethod = findApplyMethod();

        /**
         * 反射查找apply的方法
         *
         * @return
         */
        @SuppressWarnings({"unchecked", "rawtypes"})
        private static Method findApplyMethod() {
            try {
                Class clz = SharedPreferences.Editor.class;
                return clz.getMethod("apply");
            } catch (NoSuchMethodException e) {
            }

            return null;
        }

        /**
         * 如果找到则使用apply执行，否则使用commit
         *
         * @param editor
         */
        public static void apply(SharedPreferences.Editor editor) {
            try {
                if (sApplyMethod != null) {
                    sApplyMethod.invoke(editor);
                    return;
                }
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
            editor.commit();
        }
    }

    /**
     * 读取配置文件
     *
     * @param context
     * @param key
     * @param defaultValue 读取失败后返回的值
     * @return value
     */
    public static String read(Context context, String key, String defaultValue) {
        SharedPreferences spf1 = PreferenceManager.getDefaultSharedPreferences(context);
        return spf1.getString(key, defaultValue);
    }

    public static boolean read(Context context, String key, boolean defaultValue) {
        SharedPreferences spf1 = PreferenceManager.getDefaultSharedPreferences(context);
        return spf1.getBoolean(key, defaultValue);
    }

    public static float read(Context context, String key, float defaultValue) {
        SharedPreferences spf1 = PreferenceManager.getDefaultSharedPreferences(context);
        return spf1.getFloat(key, defaultValue);
    }

    public static int read(Context context, String key, int defaultValue) {
        SharedPreferences spf1 = PreferenceManager.getDefaultSharedPreferences(context);
        return spf1.getInt(key, defaultValue);
    }

    public static long read(Context context, String key, long defaultValue) {
        SharedPreferences spf1 = PreferenceManager.getDefaultSharedPreferences(context);
        return spf1.getLong(key, defaultValue);
    }
}